﻿using AutoMapper;
using System.Text.Json;
using TrackSystem.Contracts;
using TrackSystem.Entities.Dtos.Sensor;

namespace TrackSystem.UI.Server.HttpRepository.Sensor
{
    public class SensorHttpRepository : ISensorHttpRepository
    {
        private readonly HttpClient client;
        private readonly IMapper mapper;

        public SensorHttpRepository(HttpClient client,
            IMapper mapper)
        {
            this.client = client;
            this.mapper = mapper;
        }

        public async Task<IEnumerable<SensorWithChannelDto>> GetAll()
        {
            var response = await client.GetAsync("api/sensor/all/detail");
            var content = await response.Content.ReadAsStringAsync();
            var Items = JsonSerializer.Deserialize<IEnumerable<SensorWithChannelDto>>(
                content,
                new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return Items ??= [];
        }


        public async Task<ApiResult<SensorWithChannelDto?>> GetSingleSensor(int id)
        {
            var response = await client.GetAsync($"api/sensor/{id}/detail");
            var content = await response.Content.ReadAsStringAsync();
            var Item = JsonSerializer.Deserialize<ApiResult<SensorWithChannelDto>>(
                content,
                new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return Item ??= ApiResult<SensorWithChannelDto>.Fail("获取传感器错误");
        }

        public async Task<ApiResult<SensorWithChannelDto>> InsertAsync(SensorWithChannelDto sensor)
        {
            if (sensor == null)
                return ApiResult<SensorWithChannelDto>.Fail("输入为空");

            var snr = mapper.Map<SensorForCreatedDto>(sensor);
            if (snr == null)
                return ApiResult<SensorWithChannelDto>.Fail("输入数据错误");

            var response = await client.PutAsJsonAsync("api/sensor", snr);
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<ApiResult<SensorWithChannelDto>>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return result ??= ApiResult<SensorWithChannelDto>.Fail("", 500);
        }

        public async Task<ApiResult<SensorWithChannelDto>> UpdateAsync(SensorWithChannelDto sensor)
        {
            if (sensor == null)
                return ApiResult<SensorWithChannelDto>.Fail("输入为空");

            var snr = mapper.Map<SensorForUpdateDto>(sensor);
            if (snr == null)
                return ApiResult<SensorWithChannelDto>.Fail("输入数据错误");

            var response = await client.PostAsJsonAsync("api/sensor", snr);
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<ApiResult<SensorWithChannelDto>>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return result ??= ApiResult<SensorWithChannelDto>.Fail("", 500);
        }

        public async Task<ApiResult<SensorWithChannelDto>> DeleteAsync(int id)
        {
            var response = await client.DeleteAsync($"api/sensor/{id}");
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<ApiResult<SensorWithChannelDto>>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return result ??= ApiResult<SensorWithChannelDto>.Fail("", 500);
        }

    }
}
